<template>
  <div>
    <GiTable
      row-key="id"
      id="MemberEquityTable"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      @refresh="getTableData"
    >
      <template #custom-extra>
        <a-form :model="listParams" size="small" layout="inline">
          <a-form-item label="类型：">
            <a-radio-group v-model="listParams.equityDayType" @change="changeCurrent(1)">
              <a-radio :value="0">全部</a-radio>
              <a-radio :value="1">减少</a-radio>
              <a-radio :value="2">增多</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="日期：">
            <ms-date2
              v-model:start="listParams.createdAt.beginAt"
              v-model:end="listParams.createdAt.endAt"
              @dateChange="changeCurrent(1)"
            ></ms-date2>
          </a-form-item>
        </a-form>
      </template>
    </GiTable>
  </div>
</template>
<script setup>
import { userEquityDayRecordList } from '@/apis'
import { usePagination } from '@/hooks'

const route = useRoute()
const props = defineProps(['userId'])
const loading = ref(false)
const tableData = ref([])
const columns = ref([
  {
    title: '时间',
    dataIndex: 'createdAt',
    width: 180
  },
  {
    title: '权益变更（天）',
    dataIndex: 'equityDay',
    width: 180
  },
  {
    title: '剩余权益（天）',
    dataIndex: 'remainingEquityDay',
    width: 180
  },
  {
    title: '累计权益（天）',
    dataIndex: 'accumulatedEquityDay',
    width: 180
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 160
  }
])

const listParams = reactive({
  page: 1,
  pageSize: 10,
  equityDayType: undefined,
  createdAt: {
    beginAt: undefined,
    endAt: undefined
  }
})

const { pagination, setTotal } = usePagination(() => getTableData(), { defaultPageSize: listParams.pageSize })

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    listParams.page = pagination.current
    listParams.pageSize = pagination.pageSize
    const res = await userEquityDayRecordList({
      ...listParams,
      id: route.query.id || props.userId,
      createdAt: JSON.stringify(listParams.createdAt) == '{}' ? undefined : listParams.createdAt,
      equityDayType: listParams.equityDayType == 0 ? undefined : listParams.equityDayType
    })
    tableData.value = res.data.list
    setTotal(res?.data?.pagination.total || 0)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  setTimeout(() => {
    getTableData()
  }, 1000)
})

// 表格页码改变
const changeCurrent = (e) => {
  pagination.current = listParams.page = e
  getTableData()
}
</script>
